package org.example.page;

import lombok.extern.log4j.Log4j2;
import org.example.param.Config;
import org.example.param.Parameter;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.Properties;

@Log4j2
public class MyJFrame extends JFrame {

    private BufferedImage iconImage;
    private CardLayout cardLayout;
    private JPanel cardJPanel;
    private Properties config;

    public MyJFrame() throws HeadlessException {
        super(Parameter.APPLICATION_NAME);
        configInit();

        jFrameInit();

        jPanelInit();
        //托盘初始化
        trayInit();

        this.setVisible(true);
    }

    private void configInit() {
        config = Config.getConfig();
    }

    private void jPanelInit() {
        cardLayout = new CardLayout();
        cardJPanel = new JPanel(cardLayout);

        //主页面
        MainPanel mainPanel = new MainPanel(cardLayout, cardJPanel);
        //签名页面
        SignaturePanel signaturePanel = new SignaturePanel(cardJPanel, cardLayout);
        //点击重现页面
        EventReplay eventReplay = new EventReplay(cardJPanel, cardLayout);

        //批量删除页面、
        BatchDeletePanel batchDeletePanel = new BatchDeletePanel(cardJPanel, cardLayout);

        //窗口移动页面
        FrameMovePanel frameMovePanel = new FrameMovePanel(cardJPanel, cardLayout, this);

        //windows更新页面
//        WindowsErrPanel windowsErrPanel = new WindowsErrPanel(cardLayout, cardJPanel);
        //聊天界面
        ChatPanel chatPanel = null;
        boolean isChat = false;
        if ("true".equals((String) config.get("chat"))){
            chatPanel = new ChatPanel(cardLayout, cardJPanel);
            isChat = true;
        }


        //将mainPanel添加到cardJPanel中
        cardJPanel.add(mainPanel,"mainPanel");
        cardJPanel.add(signaturePanel,"signaturePanel");
        cardJPanel.add(eventReplay,"eventReplay");
        cardJPanel.add(batchDeletePanel,"batchDeletePanel");
        cardJPanel.add(frameMovePanel,"frameMovePanel");
//        cardJPanel.add(windowsErrPanel,"windowsErrPanel");
        if (isChat){
            cardJPanel.add(chatPanel,"chatPanel");
        }


        //将panel添加到Frame中
        this.add(cardJPanel);
    }

    private void trayInit() {
        if (!SystemTray.isSupported()) {
            log.info("当前系统不支持添加托盘");
            return;
        }

        SystemTray systemTray = SystemTray.getSystemTray();
        TrayIcon trayIcon = new TrayIcon(iconImage, "tool");
        //设置托盘自动大小
        trayIcon.setImageAutoSize(true);
        //设置托盘右键菜单
        trayIconAddMenu(trayIcon);
        //添加托盘点击事件
        trayIconAddClick(trayIcon);

        try {
            systemTray.add(trayIcon);
        } catch (AWTException e) {
            log.info("添加托盘失败");
            throw new RuntimeException(e);
        }


    }

    private void trayIconAddClick(TrayIcon trayIcon) {
        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                MyJFrame.this.setVisible(true);
            }
        });
    }

    private static void trayIconAddMenu(TrayIcon trayIcon) {
        PopupMenu menu = new PopupMenu("菜单");
        MenuItem item = new MenuItem("退出");
        item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        menu.add(item);
        trayIcon.setPopupMenu(menu);
    }

    private void jFrameInit() {
        this.setName(Parameter.APPLICATION_NAME);
        this.setSize(Parameter.APPLICATION_WIDTH, Parameter.APPLICATION_HEIGHT);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //窗口居中
        this.setLocationRelativeTo(null);
        //将JFrame自动失去焦点设置为false
        this.setAutoRequestFocus(false);



        URL imageUrl = this.getClass().getClassLoader().getResource("icon.png");
        iconImage = imageRead(imageUrl);
        this.setIconImage(iconImage);

    }


    private BufferedImage imageRead(URL imageUrl) {
        try {
            return ImageIO.read(imageUrl);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}